﻿using System;
using System.Windows.Data;
using System.Windows;

namespace OverlakeApp.Converters
{
#if PC
    using AppService = OverlakeWSTester.AppService;
#endif

    /// <summary>
    /// Converts from a calendar object to a string that represents its date range.
    /// </summary>
    public class CalendarItemDateConverter : IValueConverter
    {
        /// <summary>
        /// Converts from a calendar object to the string that represents the start and end times of the event.
        /// </summary>
        /// <param name="value">The calendar event.</param>
        /// <param name="targetType">Not implemented.</param>
        /// <param name="parameter">Not implemented.</param>
        /// <param name="culture">Not implemented.</param>
        /// <returns>Returns the calendar event start/end times.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is AppService.CalendarEvent)
            {
                string formatDateAndTime = "d MMMM h:mm tt";
                string formatTimeOnly = "h:mm tt";
                AppService.CalendarEvent val = (AppService.CalendarEvent)value;

                if (val.StartTime == val.EndTime)   // If it has zero length, it's all day.
                {
                    return "all day";
                }
                else
                {
                    string start = val.StartTime.ToString(formatDateAndTime);
                    string end = String.Empty;

                    if (val.StartTime.Date == val.EndTime.Date)
                    {
                        end = val.EndTime.ToString(formatTimeOnly);
                    }
                    else
                    {
                        end = val.EndTime.ToString(formatDateAndTime);
                    }

                    return (start + " to " + end).ToLower();
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// Not implemented
        /// </summary>
        /// <param name="value">Value to convert</param>
        /// <param name="targetType">Target type</param>
        /// <param name="parameter">Optional parameter</param>
        /// <param name="culture">Current culture</param>
        /// <returns>Not implemented</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
}
